package walletadminlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"strconv"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddCurrencyExchangeConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddCurrencyExchangeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCurrencyExchangeConfigLogic {
	return &AddCurrencyExchangeConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 新增钱包币种兑换配置
func (l *AddCurrencyExchangeConfigLogic) AddCurrencyExchangeConfig(in *pb.AddCurrencyExchangeConfigReq) (*pb.AddCurrencyExchangeConfigResp, error) {
	defer utils.RecoverPanic()

	cec := wallet.CurrencyExchangeConfig{
		MoneyType:    in.MoneyType,
		CurrencyType: in.CurrencyType,
		State:        dbkey.Enable,
		Id:           in.Id,
		MoneyIcon:    in.MoneyIcon,
		CurrencyIcon: in.CurrencyIcon,
		MoneyName:    in.MoneyName,
		CurrencyName: in.CurrencyName,
	}

	cec.CurrencyAmount, _ = strconv.ParseInt(in.CurrencyAmount, 10, 64)
	cec.MoneyAmount, _ = strconv.ParseInt(in.MoneyAmount, 10, 64)

	//
	_, err := l.svcCtx.CurrencyExchangeConfigModel.Insert(l.ctx, &cec)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return &pb.AddCurrencyExchangeConfigResp{}, nil
}
